package com.china08.yxyapi.schedule.attendance;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.repository.db.attendance.YxyTecAttendanceConfigRepository;
import com.china08.yxyapi.util.DateUtils;
import com.china08.yxyapi.util.RabbitMqSendUtil;
import com.china08.yxyapi.util.YxyScheduleRunnableUtil;

@Component
public class TecAttendanceStatistics {
    private static final Logger LOGGER = Logger.getLogger(TecAttendanceStatistics.class);
    @Autowired
    YxyTecAttendanceConfigRepository yxyTecAttendanceConfigRepository;

    /**
     * 考勤统计, 每天晚上 00:15:00 统计前一天数据
     */
    @Scheduled(cron = "0 15 0 * * ?")
    public void attendanceStatistics() {
        // 1: 查询哪些学校需要进行统计数据, 查询考勤配置表 首先考勤配置表中需要有配置信息才进行统计.
        // 2: 将学校id 放入到 RabbitMQ中

        // ##
        // 检测定时任务是否可执行
        if (!YxyScheduleRunnableUtil.checkRunnable()) { return; }

        Integer beforDate = DateUtils.formatDate4(DateUtils.getBeforeDay(new Date()));
        List<String> lTecAttendanceConfig = yxyTecAttendanceConfigRepository.findSchoolIdAll();

        if (lTecAttendanceConfig != null && lTecAttendanceConfig.size() > 0) {
            RabbitMqSendUtil.attendanceStatistics(lTecAttendanceConfig, beforDate);

        }
    }

    // /**
    // * 考勤统计, 每天晚上 00:15:00 统计前一天数据
    // */
    // @Scheduled(cron = "0/20 * * * * ?")
    // public void attendanceStatisticsTest() {
    // // 1: 查询哪些学校需要进行统计数据, 查询考勤配置表首先考勤配置表中需要有配置信息才进行统计.
    // // 2: 将学校id 放入到 RabbitMQ中
    // try {
    // String hostname = (InetAddress.getLocalHost()).getHostName();
    // if (!"c07-03".equalsIgnoreCase(hostname) &&
    // !"YxYunAPI01".equalsIgnoreCase(hostname)
    // && !"YxYunAPI01".equalsIgnoreCase(hostname) &&
    // !"yxapiyx01".equalsIgnoreCase(hostname)
    // && !"YxConvirt01".equalsIgnoreCase(hostname)) {
    // // // LOGGER.info("HostName:" +hostname);
    // return;
    // }
    // } catch (UnknownHostException e) {
    //
    // }
    //
    // Integer beforDate = DateUtils.formatDate4(DateUtils.getBeforeDay(new
    // Date()));
    // List<String> lTecAttendanceConfig =
    // yxyTecAttendanceConfigRepository.findSchoolIdAll();
    //
    // for (int i = 1; i <= 7; i++) {
    // if (lTecAttendanceConfig != null && lTecAttendanceConfig.size() > 0) {
    // RabbitMqSendUtil.attendanceStatistics(lTecAttendanceConfig,
    // DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
    // Date(), i)));
    // }
    // }
    //
    // for (int i = 0; i >= -7; i--) {
    // if (lTecAttendanceConfig != null && lTecAttendanceConfig.size() > 0) {
    // RabbitMqSendUtil.attendanceStatistics(lTecAttendanceConfig,
    // DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
    // Date(), i)));
    // }
    // }
    // }

    // public static void main(String[] args) {
    // Integer day =
    // DateUtils.formatDate4(org.apache.commons.lang3.time.DateUtils.addDays(new
    // Date(), 1));
    // System.out.println(day);
    //
    // }
}
